//
//  ChannelViewController.m
//  STL_ Animation
//
//  Created by DJtd on 16/3/30.
//  Copyright © 2016年 STL. All rights reserved.
//

#import "ChannelViewController.h"
#import "MusicYue_V_ViewController.h"
#import "AppDelegate.h"
#import "STL.h"
#import "ChannelBaseModel.h"
#import "STLChannelCollectionViewCell.h"

@interface ChannelViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, assign) NSInteger count;

@end

@implementation ChannelViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.dataSource = [NSMutableArray array];
    self.view.backgroundColor = [UIColor blackColor];
    self.navigationItem.title = @"频道";
    [self getData];
    [self createCollectionView];
}

#pragma mark - 数据请求
- (void)getData  {
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];

    NSString *string = @"http://mapi.yinyuetai.com/recommend/video/aggregation.json";
    [SJAFNetTool GEtNetWithUrl:string WithBody:nil headerFile:Dictionary response:SJJSON success:^(id result) {
       
        if (result) {
        NSArray *array = result[@"data"];
        for (NSDictionary *dic in array) {
            
            ChannelBaseModel *channelModel = [[ChannelBaseModel alloc] initWithDataSource:dic];
            if (![channelModel.title isEqualToString:@"爱逗秀"]) {
                [self.dataSource addObject:channelModel];
            }
            
            
        }[self.collectionView reloadData];
        [MBProgressHUD hideHUDForView:self.view animated:YES];

}
               
    } failure:^(NSError *error) {
        
        [MBProgressHUD showAlertView:@"" andMessage:@"网络错误"];
        
    }];
}


#pragma mark - 创建频道界面
- (void)createCollectionView {
   
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.itemSize = CGSizeMake(ScreenWidth/2, ScreenWidth/2);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 64) collectionViewLayout:flowLayout];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    [self.collectionView registerClass:[STLChannelCollectionViewCell class] forCellWithReuseIdentifier:@"STLChannelCollectionViewCell"];
    [self.view addSubview:self.collectionView];
    
    [self.collectionView setMj_header:[MJRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshAction)]];
}

- (void)refreshAction {
    
    [self getData];
    [self.collectionView.mj_header endRefreshing];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    STLChannelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"STLChannelCollectionViewCell" forIndexPath:indexPath];
   
    cell.channelModel = self.dataSource[indexPath.item];
    
    
    return cell;
}

- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
    
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ChannelBaseModel *channelModel = self.dataSource[indexPath.item];
    
    
    if (channelModel.idName) {
        
        MusicYue_V_ViewController *musicVc = [[MusicYue_V_ViewController alloc] init];
        musicVc.idName = channelModel.idName.stringValue;
        musicVc.type = channelModel.type;
       
        [self.navigationController pushViewController:musicVc animated:YES];
    }
}



@end
